<template>
    <div>
        <h3>上架/下架</h3>
        <span>商品名</span>
        <el-input class="p1" v-model="goodsNames" placeholder="请输入商品名"></el-input>
        <span id="ss">商品状态</span>
        <el-input class="p2" v-model="states" placeholder="请输入状态"></el-input>
        <el-button id="ff" @click="findByItem">查询</el-button>
        <el-table
                :data="tableData.slice((currentPage-1)*PageSize,currentPage*PageSize)" style="width: 100%">
            <el-table-column
                    prop="code"
                    label="商品编号">
            </el-table-column>
            <el-table-column
                    prop="goodsName"
                    label="商品名称">
            </el-table-column>
            <el-table-column
                    prop="batch"
                    label="批次">
            </el-table-column>
            <el-table-column
                    prop="stockNum"
                    label="库存数量">
            </el-table-column>
            <el-table-column
                    prop="type"
                    label="商品类型">
            </el-table-column>
            <el-table-column
                    prop="specs"
                    label="商品规格">
            </el-table-column>
            <el-table-column
                    prop="sellPrice"
                    label="售价">
            </el-table-column>
            <el-table-column
                    prop="state"
                    label="状态">
            </el-table-column>
            <el-table-column
                    fixed="right"
                    label="操作">
                <template slot-scope="scope">
                    <el-button :disabled="scope.row.state == '0' ? true : (scope.row.state == '2' ? true : false)" @click="upper(scope.row)" type="text" size="small">上架</el-button>
                    <el-button :disabled="scope.row.state == '2' ? false : true" @click="cancel(scope.row)" type="text" size="small">下架</el-button>
                </template>
            </el-table-column>

        </el-table>
        <div class="tabListPage">
            <div class="tabListPage">
                <el-pagination @size-change="handleSizeChange"
                               @current-change="handleCurrentChange"
                               :current-page="currentPage"
                               :page-sizes="pageSizes"
                               :page-size="PageSize" layout="total, sizes, prev, pager, next, jumper"
                               :total="totalCount">
                </el-pagination>
            </div>
            <el-button type="text"  size="small" id="bu" @click="finds">查看上架商品</el-button>
        </div>
    </div>
</template>

<script>
    export default {
        name: "BatchView",
        data() {
            return {
                tableData:[],
                goodsNames: "",
                states:"",
                totalCount:null,
                currentPage:1,
                pageSizes:[1,2,3,4],
                PageSize:3,
                state:"",
            }
        },
        mounted() {
            this.init()
        },
        methods: {
            handleSizeChange(val) {
                this.PageSize=val
                this.currentPage=1
            },
            handleCurrentChange(val) {
                this.currentPage=val
            },
            findByItem(){
                this.$axios.get("/sscsys/shelves/findByItem",{
                    params:{
                         name:this.goodsNames,
                         state:this.states
                    }
                }).then(resp =>{
                    this.tableData = resp.data
                })
            },
            init(){
                this.$axios.get("/sscsys/shelves/findAll").then(resp =>{
                    this.tableData=resp.data;
                    this.totalCount=resp.data.length;
                })
            },

            finds(){
                this.$router.push("goodsShelvesViews")
            },
            upper(row){
                this.$prompt('请输入数量', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    inputErrorMessage: '格式不正确'
                }).then(({ value }) => {
                    this.$axios.get("/sscsys/shelves/add",{
                        params:{
                            goodsname:row.goodsName,
                            goodscode:row.code,
                            number:value,
                            goodstype:row.type,
                            goodspricce:row.sellPrice,
                            goodsspecs:row.specs,
                            goodsstate:"0",
                            goodsbatch:row.batch
                        }
                    }).then(resp =>{
                        if(resp.data == ""){
                            console.log("添加成功")
                            row.state = "2"


                            // this.$axios.get("apc/shelves/updateGoodsNum",{
                            //     params:{
                            //         batch:row.batch,
                            //         num:value
                            //     }
                            // }).then(resp =>{
                            //    if(resp.data == "ok"){
                            //      console.log("修改成功")
                            //    }
                            //    else {
                            //        console.log("修改失败")
                            //    }
                            // })
                        }
                        else {
                            console.log("添加失败")
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '取消输入'
                    });
                });
            },
            cancel(row){
                 this.$axios.get("/sscsys/shelves/upGoodsStatue",{
                     params:{
                         code:row.code
                     }
                 }).then(resp =>{
                      if(resp.data == "ok"){
                          this.$axios.get("/sscsys/shelves/del",{
                              params:{
                                  code:row.code
                              }
                          }).then(resp =>{
                              if(resp.data =='ok'){
                                  console.log("下架成功")
                                  row.state="0"
                              }
                              else {
                                  console.log("下架失败")
                              }
                          })
                      }
                      else {
                          console.log("修改错误")
                      }
                 })
            },

        }

    }
</script>
<style scoped>
    #bu{
        position: relative;
        left: 900px;
    }
    .p1{
        left: 30px;
        width: 200px
    }
    .p2{
        left: 60px;
        width: 200px
    }
    #ss{
        padding-left: 100px;
    }
    #ff{
        position: relative;
        left: 100px;
    }
</style>
